package com.bdmc.smokesensor.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.Utils.WebTokenUtil;
import com.bdmc.smokesensor.Utils.redis.RedisUtil;
import com.bdmc.smokesensor.Utils.wechatsdk.AccessToken;
import com.bdmc.smokesensor.Utils.wechatsdk.OAuthUser;
import com.bdmc.smokesensor.Utils.wechatsdk.WeChatUtil;
import com.bdmc.smokesensor.annotation.AuthIgnore;
import com.bdmc.smokesensor.entity.Smk_Buildings;
import com.bdmc.smokesensor.entity.Sys_User;
import com.bdmc.smokesensor.iservices.ISmk_BuildingsService;
import com.bdmc.smokesensor.iservices.ISys_UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/wechat/oauth")
public class WeChatController {

    @Autowired
    public RedisUtil _redis;

    @Autowired
    private ISmk_BuildingsService _bdSvs;

    @Autowired
    private ISys_UserService _userSvs;

    @PostMapping(value = "/set")
    public Msg test1(@RequestBody Map<String, Object> map) {

        String token = "";
        if (token == null || token.isEmpty()) {
            AccessToken at = WeChatUtil.getAccessToken();
            token = at.getToken();
            _redis.set("token", token);
        }
        return ResultUtil.success(token);
    }

    @AuthIgnore
    @PostMapping(value = "/create")
    public Msg create(@RequestBody Map<String, Object> map) {

        String rurl = map.get("call_back").toString();
        String result = WeChatUtil.CreateRedirect(rurl);
        return ResultUtil.success(result);
    }

    @AuthIgnore
    @PostMapping(value = "/getuser")
    public Msg getUser(@RequestBody Map<String, Object> map) {
        String code = map.get("code").toString();
        int user_type = Integer.valueOf(String.valueOf(map.get("user_type")));
        OAuthUser result = WeChatUtil.GetOauthUser(code);
        if (result.openid != null) {
            if (user_type == 1) {// 1用户
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                List<Smk_Buildings> buildings = _bdSvs.findByOpenid(result.openid);
                if (buildings.size() > 0) {// 微信号已绑定店铺 则返回店铺信息
                    Smk_Buildings b = buildings.get(0);
                    String token = WebTokenUtil.createJWT(buildings.get(0).getId(), buildings.get(0).getOpenid(),
                            buildings.get(0).getDepartmentId(), buildings.get(0).getWxName(), 36000000);
                    List<Map<String, Object>> shopInfo = new ArrayList<Map<String, Object>>();
                    for (Smk_Buildings item : buildings) {
                        Map<String, Object> m = new HashMap<String, Object>();
                        m.put("shopName", item.getName());
                        m.put("addr", item.getAddr());
                        shopInfo.add(m);
                    }
                    respMap.put("mobile", b.getManagerMobile());
                    respMap.put("managerName", b.getManagerName());
                    respMap.put("shopInfo", shopInfo);
                    respMap.put("token", token);
                } 
                return ResultUtil.success(respMap);
            }else if(user_type == 2){//type=2是维保
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                Sys_User u= _userSvs.findByOpenidAndRoleNum(result.getOpenid(), "2");//2是维保
                if(u!=null){
                    String token = WebTokenUtil.createJWT(u.getId(), u.getOpenid(),
                    u.getDepartmentId(), u.getWxName(), 36000000);
                    respMap.put("dep_id", u.getDepartmentId());
                    respMap.put("mobile", u.getMobile());
                    respMap.put("loginName", u.getLoginName());
                    respMap.put("nickName", u.getNickname());
                    respMap.put("token", token);
                }
                return ResultUtil.success(respMap);
            }else if(user_type == 3){//3是管理员
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                Sys_User u= _userSvs.findByOpenidAndRoleNum(result.getOpenid(), "1");//rolenum = 1是管理员
                if(u==null){
                    u= _userSvs.findByOpenidAndRoleNum(result.getOpenid(), "3");//rolenum = 3是政府检查
                }
                if(u==null){
                    u= _userSvs.findByOpenidAndRoleNum(result.getOpenid(), "5");//rolenum = 3是政府检查
                }
                if(u!=null){
                    String token = WebTokenUtil.createJWT(u.getId(), u.getOpenid(),
                    u.getDepartmentId(), u.getWxName(), 36000000);
                    respMap.put("dep_id", u.getDepartmentId());
                    respMap.put("mobile", u.getMobile());
                    respMap.put("loginName", u.getLoginName());
                    respMap.put("nickName", u.getNickname());
                    respMap.put("token", token);
                }
                return ResultUtil.success(respMap);
            }
            
        } else {
            return ResultUtil.error(-1, "code过期。");
        }
        return ResultUtil.success();
    }

}